var async = require('async')

module.exports = function(app) {
  return new Handler(app)
}

var Handler = function(app) {
  this.app = app
  Handler.app = app
  this.sessionService = this.app.get('sessionService')
  this.channelService = this.app.get('channelService')
}

var handler = Handler.prototype
var local = {}


//创建茶楼
handler.createTeaHouse = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	this.app.rpc.db.teaHouse.createTeaHouse(null,uid,false,function(flag,data){
		console.log(flag)
		console.log(data)
		next(null,{flag : flag,roomId : data})
	})
}
//管理员创建茶楼
handler.m_createTeaHouse = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	var targetUid = msg.uid
	var self = this
	self.app.rpc.db.remote.getValue(null,uid,"limits",function(data) {
		if(data >= 5){
			self.app.rpc.db.teaHouse.createTeaHouse(null,targetUid,true,function(flag,data){
				console.log(flag)
				console.log(data)
				next(null,{flag : flag,data : data})
			})
		}else{
			next(null,{flag : false})
		}
	})
}

//关闭茶楼
handler.closeTeaHouse = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	var self = this
	self.app.rpc.db.remote.getValue(null,uid,"limits",function(data) {
		if(data >= 5){
			self.app.rpc.db.teaHouse.closeTeaHouse(null,msg.uid,function(flag,data){
				console.log(flag)
				console.log(data)
				next(null,{flag : flag,data : data})
			})
		}else{
			next(null,{flag : false})
		}
	})
}

//获取茶楼配置
handler.getTeaCfg = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	this.app.rpc.db.teaHouse.getTeaCfg(null,uid,function(flag,data) {
		next(null,{flag : flag,data : data})
	})
}

//获取茶楼列表
handler.getTeaList = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	var self = this
	self.app.rpc.db.remote.getValue(null,uid,"limits",function(data) {
		if(data >= 5){
			self.app.rpc.db.teaHouse.getTeaList(null,function(flag,data){
				console.log(flag)
				console.log(data)
				next(null,{flag : flag,data : data})
			})
		}else{
			next(null,{flag : false})
		}
	})
}

//加入茶楼，获取茶楼数据
handler.joinTeaHouse = function(msg,session,next) {
	if(!msg.teaId || typeof(msg.teaId) != "number"){
		next(null,{flag : false})
		return
	}
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	this.app.rpc.db.teaHouse.joinTeaHouse(null,uid,this.app.get('serverId'),msg.teaId,function(flag,data){
		console.log(flag)
		console.log(data)
		next(null,{flag : flag,msg : data})
	})
}

//获取桌子数据
handler.getTableInfo = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	this.app.rpc.db.teaHouse.getTableInfo(null,uid,function(flag,data){
		console.log(flag)
		console.log(data)
		next(null,{flag : flag,data : data})
	})
}

//离开茶楼
handler.leaveTeaHouse = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	this.app.rpc.db.teaHouse.leaveTeaHouse(null,uid,function(flag,data){
		console.log(flag)
		console.log(data)
		next(null,{flag : flag,data : data})
	})
}

//加入桌子
handler.joinTeaTable = function(msg,session,next) {
	if(!msg.tableId || typeof(msg.tableId) != "number"){
		console.log("msg error")
		console.log(msg)
		next(null,{flag : false})
		return
	}
	var uid = session.get("uid")
	if(!uid){
		console.log("uid error"+uid)
		next(null,{flag : false})
		return
	}
	if(msg){
		msg.ip = this.sessionService.getClientAddressBySessionId(session.id).ip   
	}
	this.app.rpc.db.teaHouse.joinTeaTable(null,uid,this.app.get('serverId'),msg.tableId,msg.ip,function(flag,data){
		console.log(flag)
		console.log(data)
		next(null,{flag : flag,msg : data})
	})
}


//设置桌子配置
handler.setTeaTableInfo = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	if(!msg.tableId || typeof(msg.tableId) != "number"){
		console.log("tableId err : "+msg.tableId)
		next(null,{flag : false})
		return
	}
	this.app.rpc.db.teaHouse.setTeaTableInfo(null,uid,msg.tableId,msg.info,function(flag,data){
		console.log(flag)
		console.log(data)
		next(null,{flag : flag,data : data})
	})
}

//解散桌子
handler.dissolvedTable = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	if(!msg.tableId || typeof(msg.tableId) != "number"){
		console.log("tableId err : "+msg.tableId)
		next(null,{flag : false})
		return
	}
	this.app.rpc.db.teaHouse.dissolved(null,uid,msg.tableId,function(flag,data){
		next(null,{flag : flag,data : data})
	})
}

//获取我的茶楼
handler.getMyTeaHouse = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	this.app.rpc.db.teaHouse.getMyTeaHouse(null,uid,function(flag,data){
		console.log(flag)
		console.log(data)
		next(null,{flag : flag,data : data})
	})
}

//设置自定义数据
handler.setTeaDefined = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	this.app.rpc.db.teaHouse.setTeaDefined(null,uid,msg.obj,function(flag){
		next(null,{flag : flag})
	})
}

handler.openTable = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	this.app.rpc.db.teaHouse.openTable(null,uid,function(flag,data){
		console.log(flag)
		console.log(data)
		next(null,{flag : flag,data : data})
	})
}

//获取茶楼排行榜
handler.getTeaRankingList = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	this.app.rpc.db.teaHouse.getTeaRankingList(null,uid,msg.type,msg.code,function(data) {
		if(data){
			next(null,{flag : true,data : data})
		}else{
			next(null,{flag : false,data : data})
		}
	})
}

//获取茶楼战绩
handler.getTeaRecord = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	this.app.rpc.db.teaHouse.getTeaRecord(null,uid,msg.begin,msg.end,function(data) {
		if(data){
			next(null,{flag : true,data : data})
		}else{
			next(null,{flag : false,data : data})
		}
	})
}


//获取茶楼经营数据
handler.getRunData = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	this.app.rpc.db.teaHouse.getRunData(null,uid,function(flag,data) {
		next(null,{flag : flag,data : data})
	})
}


//管理员获取茶楼经营数据
handler.m_getRunData = function(msg,session,next) {
	var targetUid = msg.uid
	if(!targetUid || typeof(targetUid) !== "number"){
		next(null,{flag : false})
		return
	}
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	var self = this
	self.app.rpc.db.remote.getValue(null,uid,"limits",function(data) {
		if(data >= 5){
			self.app.rpc.db.teaHouse.getRunData(null,targetUid,function(flag,data){
				next(null,{flag : flag,data : data})
			})
		}else{
			next(null,{flag : false})
		}
	})
}

//管理员设置茶楼配置
handler.setTeaCfg = function(msg,session,next){
	if(!msg.cfg || typeof(msg.cfg) !== "object"){
		next(null,{flag : false})
		return
	}
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	var self = this
	self.app.rpc.db.remote.getValue(null,uid,"limits",function(data) {
		if(data >= 5){
			self.app.rpc.db.teaHouse.setTeaCfg(null,msg.cfg,function(flag,data){
				next(null,{flag : flag})
			})
		}else{
			next(null,{flag : false})
		}
	})
}

//设置茶楼信息（名字、公告）
handler.setTeaInfo = function(msg,session,next) {
	if(!msg.name || typeof(msg.name) !== "string" || typeof(msg.notify) !== "string"){
		console.log("name : "+msg.name)
		console.log("notify : "+msg.notify)
		next(null,{flag : false})
		return
	}
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	this.app.rpc.db.teaHouse.setTeaInfo(null,uid,msg.name,msg.notify,function(flag,data){
		next(null,{flag : flag})
	})
}